package com.ruicar.afs.cloud.apply.pre.loan.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.apply.business.service.ApplyAdminService;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.loan.condition.CarDealerCondition;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyChannelInfoService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 渠道
 * @author: sijun.yu
 * @date: 2020/5/28 14:28
 */
@Slf4j
@RestController
@RequestMapping("/channel")
@AllArgsConstructor
public class ApplyChannelController {

    private ApplyChannelInfoService channelInfoService;
    private ApplyAdminService applyAdminService;

    /**
     * 查询车商列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getCarDealList")
    @ApiOperation("查询车商列表")
    public IResponse getCarDealList(@ModelAttribute CarDealerCondition condition) {

        condition.setUserId(SecurityUtils.getUsername());
        JSONObject object = SecurityUtils.getUser().getUserExtInfo().getJSONObject("channelInfo");
        if (ApplyConstants.CHANNEL_BELONG_DIRECT.equals(object.getString("channelBelong"))) {
            condition.setCardealerType(ApplyConstants.DEALER_TYPE_DIRECT);
        } else {
            condition.setCardealerType(ApplyConstants.DEALER_TYPE_ORDINARY);

        }
        condition.setChannelId(SecurityUtils.getUser().getUserExtInfo().getString("channelId"));
        return channelInfoService.getCarDealList(condition);
    }

    /**
     * 查询挂靠公司列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getAffiliatedUnitsList")
    @ApiOperation("查询挂靠公司列表")
    public IResponse getAffiliatedUnitsList(@ModelAttribute CarDealerCondition condition) {

        condition.setChannelId(SecurityUtils.getUser().getUserExtInfo().getString("channelId"));
        return channelInfoService.getAffiliatedUnitsList(condition);
    }

    /**
     * 查询网约车平台列表
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getCooperPlatList")
    @ApiOperation("查询网约车平台列表")
    public IResponse getCooperPlatList(@ModelAttribute CarDealerCondition condition) {

        return channelInfoService.getCooperPlatList(condition);
    }

    /**
     * 通过渠道ID获取渠道信息
     * @param condition
     * @return
     */
    @PostMapping(value = "/channelInfoForUserName")
    @ApiOperation("通过渠道ID获取渠道信息")
    public IResponse channelInfoForUserName(@RequestBody CarDealerCondition condition){

        String channelId = condition.getChannelId();
        JSONObject jsonObject = channelInfoService.getChannelDetailInfo(channelId);
        if (CommonConstants.SUCCESS.equals(jsonObject.getString("code"))) {
            return IResponse.success(jsonObject.getJSONObject("data"));
        } else {
            return IResponse.fail("未匹配到对应渠道信息");
        }
    }

    /**
     * 通过渠道id返回渠道和车商列表
     * @param condition
     * @return
     */
    @PostMapping(value = "/getCarDealerByChannelId")
    @ApiOperation("通过渠道id返回渠道和车商列表")
    public IResponse getCarDealerByChannelId(@RequestBody CarDealerCondition condition){

        IResponse iResponse = applyAdminService.getChannelIdByUsername(condition.getUserId());
        String channel = iResponse.getData().toString();
        return channelInfoService.getCarDealerByChannelId(channel);
    }

}
